package relu.fbs.xt;

import java.util.Map;

import jfm.model.Types.CropType;
import relu.fbs.BadDataException;
import relu.fbs.xt.FBSCropData.FBSCDatum;
import relu.fbs.xt.FBSFarmData.FBSFarmDatum;
import relu.fbs.FarmData;

public class FBSPost2000DataReader {
	private final Map<CropType,FBSCropData> crops;
	private final FBSFarmData parent;
	private final int farmId;
	public FBSPost2000DataReader(int fid,Map<CropType,FBSCropData> cps,FBSFarmData par){
		farmId=fid;
		crops=cps;
		parent=par;
	}
	
	public boolean addDatum(FBSPost2000DataPoint ident,double value) throws BadDataException {
		try { 
		switch ( ident.section){
		case 'A':
			FBSDataReader.addSectionAData(parent,ident,value);
			break;
		case 'B':
			FBSDataReader.addSectionBData(parent,ident,value);
			break;
		case 'C':
			addSectionCData(ident,value);
			break;
		}		
		} catch (BadDataException ex){
//			throw new BadDataException(dumpAll()+"\n"+ex.getMessage()+" for farm "+farmId+"\n ");
			throw new Error(ex.getMessage()+" for farm "+farmId+"\n ");
		}
		return true;
	}
	

	private void ignoreCropData(FBSPost2000DataPoint ident,FBSCropData cdata,int col,double value){
		switch(col){
		case 50:
			cdata.flagBadCrop(); // Has missing data
			break;	
		case 58:
			System.out.println("Warning ignoring column "+col+" "+value+" "+ident.ctype);
			break;
		case 1:
			System.out.println("Warning ignoring column "+col+" "+value+" "+ident.ctype);
			break;
		}
	}
	private void ignoreCropType(int ct,FBSCropData cdata,FBSPost2000DataPoint ident,double value) throws BadDataException {
		 // for the moment really just do nothing .. 
	}
	private void addSectionCData(FBSPost2000DataPoint ident,double value) throws BadDataException {
		if ( FarmData.fbsCodeToCropType.containsKey(ident.row)){
			CropType cropType=FarmData.fbsCodeToCropType.get(ident.row);
			FBSCropData cdata;
			if ( crops.containsKey(cropType)){
				cdata=crops.get(cropType);
			} else {
				throw new Error("Only crops predefined in static block area allowed");
			}
			switch ( ident.ctype){
			case 0:
				ignoreCropType(0,cdata,ident,value);
				break;
			case 1:
				addMainCropData(cdata,ident,value);
				break;
			case 2:
				addMainCropData(cdata,ident,value);
				break;
			case 3:
				ignoreCropType(3,cdata,ident,value);
				break;
			case 4:
				addMainCropData(cdata,ident,value);
				break;
			case 5:
				addMainCropData(cdata,ident,value);
				break;
			case 6:
				ignoreCropType(6,cdata,ident,value);
				break;
			case 7:
				ignoreCropType(7,cdata,ident,value);
				break;
			case 8:
				addSetAsideData(cdata,ident,value);
				break;
			case 9:
				ignoreCropType(9,cdata,ident,value);
				break;	
			case 10:
				ignoreCropType(10,cdata,ident,value);
				break;	
			default:
				throw new BadDataException("Unknown crop type code "+ident.ctype);
			}
		} else {
			if (!FBSDataReader.addSectionCTotal(parent,ident,value)){
				throw new BadDataException("Unknown Crop with code "+ident.row);				
			}
		}
	}
	
	private void addSetAsideData(FBSCropData cdata,FBSPost2000DataPoint ident,double value) throws BadDataException {
		switch ( ident.col ){
		case 21:
			cdata.setDatum(FBSCDatum.AREASA, value,ident);
			break;
		case 22:
			cdata.setDatum(FBSCDatum.AREASA, value,ident);
			break;
		case 23:
			cdata.setDatum(FBSCDatum.PRODUCTIONSA, value, ident);
			break;
		case 24:
			cdata.setDatum(FBSCDatum.YIELDSA, value, ident);
			break;
		case 27:
			cdata.setDatum(FBSCDatum.TREMAINSA, value,ident);
			break;
		case 28:
			cdata.setDatum(FBSCDatum.VREMAINSA, value,ident);
			break;
		case 29:
			cdata.setDatum(FBSCDatum.TSOLDSA, value, ident);
			break;
		case 33:
			ignoreCropData(ident,cdata,33,value); // Feed
			break;
		case 34:
			ignoreCropData(ident,cdata,34,value); // Feed
			break;
		case 30:
			cdata.setDatum(FBSCDatum.VSOLDSA, value, ident);
			break;
		case 41:
			cdata.setDatum(FBSCDatum.EOSA, value,ident);
			break;
		case 39:
			cdata.setDatum(FBSCDatum.AREAENTITLEMENTSA, value,ident);
			break;
		case 50:
			ignoreCropData(ident,cdata,50,value);
			break;
		case 53:
			ignoreCropData(ident,cdata,53,value);
			break;
		case 58:
			ignoreCropData(ident,cdata,58,value);
			break;
		case 25:
			// Opening valuation
			break;
		case 26:
			// Opening valuation
			break;
			
		default:				
			throw new BadDataException("Unknown column value "+ident.col+" when adding setaside data");
		}
	}
	
	private void addMainCropData(FBSCropData cdata,FBSPost2000DataPoint ident,double value) throws BadDataException {
		switch ( ident.col){
		case 21:
			cdata.setDatum(FBSCDatum.AREA, value,ident);
			break;
		case 22:
			cdata.setDatum(FBSCDatum.MAREA, value,ident);
			break;
		case 23:
			cdata.setDatum(FBSCDatum.PRODUCTION, value,ident);
			break;
		case 24:
			cdata.setDatum(FBSCDatum.YIELD, value,ident);
			break;
		case 27:
			cdata.setDatum(FBSCDatum.TREMAIN, value,ident);
			break;
		case 28:
			cdata.setDatum(FBSCDatum.VREMAIN, value,ident);
			break;
		case 29:
			cdata.setDatum(FBSCDatum.TSOLD, value,ident);
			break;
		case 30:
			cdata.setDatum(FBSCDatum.VSOLD, value,ident);
			break;
		case 31:
			cdata.setDatum(FBSCDatum.TONFARMCONS, value,ident);
			break;
		case 32:
			cdata.setDatum(FBSCDatum.VONFARMCONS, value,ident);
			break;
		case 33:
			cdata.setDatum(FBSCDatum.TFEED, value,ident);
			break;
		case 34:
			cdata.setDatum(FBSCDatum.VFEED, value,ident);
			break;
		case 38:
			cdata.setDatum(FBSCDatum.VONFARMCONS,value,ident);
			break;
		case 39:
			cdata.setDatum(FBSCDatum.AREAENTITLEMENT, value,ident);
			break;
		case 40:
			cdata.setDatum(FBSCDatum.AREAPAYMENT, value,ident);
			break;
		case 41:
			cdata.setDatum(FBSCDatum.EO, value,ident);
			break;	
		case 50:
			ignoreCropData(ident,cdata,50,value);
			break;
		case 51:
			ignoreCropData(ident,cdata,51,value);
			break;
		case 53:
			ignoreCropData(ident,cdata,53,value);
			break;
		case 58:
			ignoreCropData(ident,cdata,58,value);
			break;
		case 25:
			// Opening valuation
			break;
		case 26:
			// Opening valuation
			break;
		case 1:
			ignoreCropData(ident,cdata,1,value);
			break;
		default:
			throw new BadDataException("Unknown column value "+ident.col);
		}
	}
	
	

	
	
}
